package cn.lil.servlet;

import cn.lil.model.User;
import cn.lil.service.UserService;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;

@WebServlet(name = "UserRegisterServlet", value = "/UserRegisterServlet")
public class UserRegisterServlet extends HttpServlet {
    UserService uService = new UserService();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String email = request.getParameter("email");
        int id=1;
        User user = new User(id,username, password, email,null,null);
        System.out.println(user);
        if (username==null||username.isEmpty()){
            request.setAttribute("failMsg","请输入正确的账号和密码");
            request.getRequestDispatcher("login.jsp").forward(request,response);
        }else {
            if (uService.register(user)) {
                request.setAttribute("failMsg", "您已成功注册，前往登陆吧");
                request.getRequestDispatcher("login.jsp").forward(request, response);
            }else {
                System.out.println("111");
                request.setAttribute("failMsg", "用户已存在");
                request.getRequestDispatcher("login.jsp").forward(request, response);
            }
        }
    }
}
